﻿'use strict';
app.controller('WalletController', function ($rootScope, $scope, $state, $localStorage, $modal, $stateParams, WalletFactory, commonFactory) {
    $scope.title = "Wallet Manager",
    $scope.messeger = "",
    $scope.popup = function (windowClass, StyleModal, Controller, size) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: StyleModal,
            controller: Controller,
            size: size,
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    },
    $scope.PaymentMesseger = "",
    $scope.WalletList = [],
    $scope.PeddingCashs = [],
    $scope.CashoutCompleted = [],
    getResultPerfectMoney(),
    getMyWallet($localStorage.user.UserId),
    getPeddingCash($localStorage.user.UserId),
    getCashoutCompleted($localStorage.user.UserId);
    function getMyWallet(UserId) {
        WalletFactory.getMyWallet(UserId).then(function (response) {
            $scope.WalletList = response.data;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    function getPeddingCash(UserId) {
        WalletFactory.getPeddingCash(UserId).then(function (response) {
            $scope.PeddingCashs = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    function getCashoutCompleted(UserId) {
        WalletFactory.getCashoutCompleted(UserId).then(function (response) {
            $scope.CashoutCompleted = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    function getResultPerfectMoney() {
        switch (parseInt($stateParams.result)) {
            case 1:
                $scope.PaymentMesseger = "Add fund successfully";
                break;
            case -1:
                $scope.PaymentMesseger = "Store database not successful";
                break;
            case -2:
                $scope.PaymentMesseger = "Hash invalid";
                break;
            case -3:
                $scope.PaymentMesseger = "form invalid";
                break;
        }
    }

})

app.controller('WalletModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, messeger, title) {
    $scope.messeger = messeger,
    $scope.title = title;
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});

app.controller('WalletAddModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $state, messeger, title, commonFactory) {
    $scope.messeger = messeger,
    $scope.title = "Add",
    $scope.payment = {},
    $scope.PaymentMethod = [],
    $scope.selected = [],
    $scope.processing = false,
    GetPaymentMethod();
    $scope.doAddPack = function (item) {
        $scope.processing = true;
        if (item.Amount > 0 && item.PaymentId > 0) {
            $state.go("app.payment", { id: item.PaymentId, amount: item.Amount })
            $modalInstance.close();
        } else {
            $scope.messeger = "Amount field or Payment field required.";
        }
    }
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
    function GetPaymentMethod() {
        commonFactory.GetPaymentMethod().then(function (response) {
            $scope.PaymentMethod = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }

});

app.controller('WalletCashoutModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $http, $rootScope, $timeout, $state, $stateParams, messeger, title, commonFactory, WalletFactory, BitcoinFactory) {

    $scope.messeger = messeger,
    $scope.title = title,
    $scope.processing = false,
    $scope.AlertType = "warning";
    $scope.rate = 0,
    $scope.payment = {},
    $scope.wallet = {},
    $scope.PaymentMethod = [],
    $scope.selected = [],
    $scope.FeeConfig = {},
    $scope.usdValue = 0,
    $scope.data = { Amount: "", Fee: 0, After: 0 };
    //get fee cashout dynamit
    $scope.doProcess = function (data) {
        $scope.messeger = "";
        //if (parseFloat(data.Amount) > 3000000)
        //{
        //    $scope.data.After = data.Amount - (data.Amount * data.Fee);
        //    $scope.usdValue = data.Amount * $scope.rate;
        //} else {
        //    $scope.data.After = 0;
        //    $scope.messeger = "Amount must greater > 3,000,000";
        //}
            $scope.data.After = data.Amount - (data.Amount * data.Fee);
            $scope.usdValue = data.Amount * $scope.rate;
    },
    $scope.doCashout = function (item) {
        $scope.processing = true,
        $scope.ExistSession = false,
        $scope.messeger = "";
        if (item.Amount > 0 && item.PaymentId > 0 && (item.PinCode != null || item.PinCode!="")) {
            commonFactory.CheckSession($localStorage.user.SessionId).then(function (resp) {
                if (resp.data.Status) {
                    //do prosess
                    Cashout(item)
                } else {
                    $localStorage.user = null;
                    $scope.messeger = "Session is expired";
                    $timeout(function () {
                        $state.go('login');
                    }, 5000);
                }
            }, function (error) {
                $scope.status = 'Unable to check session data: ' + error.message;
            });

        } else {
            $scope.processing = false,
            $scope.messeger = "Error: Amount >= 0,";
        }
    },
    GetBitcointoUSDRate(1),
    GetPaymentMethod(),
    getFeeConfig(),
    NotFundingWalletList($localStorage.user.UserId);
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
    //check session   
    function NotFundingWalletList(UserId) {
        commonFactory.GetNotFundingWalletList(UserId).then(function (response) {
            $scope.WalletList = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    };
    function GetPaymentMethod() {
        commonFactory.GetPaymentMethod().then(function (response) {
            $scope.PaymentMethod = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    };
    function getFeeConfig() {
        WalletFactory.getFeeConfig().then(function (response) {
            $scope.data.Fee = response.data.Result.Cashout;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    };
    function Cashout(item) {
        $http.post($rootScope.APITransactionUrl + 'CashOutNotApprove', {
            userid: $localStorage.user.UserId,
            amount: item.Amount,
            payment: item.PaymentId,
            walletId: item.Wallet,
            pinCode: item.PinCode
        }).then(function successCallback(resp) {
            $scope.processing = false;
            if (resp.data.Status) {
                $scope.AlertType = "success";
                $state.transitionTo($state.current, $stateParams, {
                    reload: true,
                    inherit: false,
                    notify: true
                }),
                $timeout(function () {
                    $modalInstance.close();
                }, 5000);
            } else {
                $scope.AlertType = "warning";
            }
            $scope.messeger = resp.data.Messeger;
        })
    };
    function GetBitcointoUSDRate(Bitcoin) {
        BitcoinFactory.GetBitcointoUSDRate(Bitcoin).then(function (response) {
            $scope.rate = response.data;
        }, function (error) {
            $scope.status = 'Unable to load rate data: ' + error.message;
        });
    };

});